package vn.edu.vttu.nhanvien;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import net.proteanit.sql.DbUtils;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import vn.edu.vttu.readxml.ReadXML;
import vn.edu.vttu.readxml.thongtindb;

public class NhanVien_QuanLy extends javax.swing.JFrame {

    private ResultSet rs;
    private PreparedStatement psr;
    private boolean them = false;

    public NhanVien_QuanLy() {
        initComponents();
        setLocation(150, 20);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.loadData();
        enableTxt(false);
        enableButton(true);
    }

    public void loadData() {
        try {
            ReadXML read = new ReadXML();
            Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
            vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
            TableModel x = common.loadDataNhanVien();
            this.tblDSNhanVien.setModel(x);
            this.tblDSNhanVien.setRowSelectionInterval(0, 0);
            txtMaNV.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 0)));
        txtHoTen.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 1)));                               
        dtNgaysinh.setDate((Date) tblDSNhanVien.getValueAt(0, 2));
        String gt = String.valueOf(tblDSNhanVien.getValueAt(0, 3));
        if (gt.equals("Nam")) {
            cobGioiTinh.setSelectedItem("Nam");
        } else {
            cobGioiTinh.setSelectedItem("Nữ");
        }
        txtDiaChi.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 4)));
        txtEmail.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 6)));
        txtsdt.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 5)));
        txtcmnd.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 7)));
        dtNgayvaolam.setDate((Date) tblDSNhanVien.getValueAt(0, 8));
        txtGhiChu.setText(String.valueOf(tblDSNhanVien.getValueAt(0, 9)));
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, e);
        }
    }// load data

    private void enableTxt(boolean b) {
        txtHoTen.setEnabled(b);
        txtDiaChi.setEnabled(b);
        txtsdt.setEnabled(b);
        txtEmail.setEnabled(b);
        txtcmnd.setEnabled(b);
        txtGhiChu.setEnabled(b);
        cobGioiTinh.setEnabled(b);
        dtNgaysinh.setEnabled(b);
        dtNgayvaolam.setEnabled(b);
        txtTimKiem.setEnabled(!b);
    }

    private void enableButton(boolean b) {
        btnThem.setEnabled(b);
        btnSua.setEnabled(b);
        btnXoa.setEnabled(b);
        btnLuu.setEnabled(!b);
        tblDSNhanVien.setEnabled(b);
    }

    private String now() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date now = new Date(System.currentTimeMillis());
        return sdf.format(now);
    }

    private int getAge(Date birthdate) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(new Date());
        int currentYear = calendar.get(Calendar.YEAR);
        calendar.setTime(birthdate);
        int birthYear = calendar.get(Calendar.YEAR);
        return currentYear - birthYear;
    }

    private boolean compareDate(Date ngay1, Date ngay2) {
        int x = (ngay1.getYear()+1900) * 12 * 31 + (ngay1.getMonth()+1) * 31 + ngay1.getDate();
        int y = (ngay2.getYear()+1900) * 12 * 31 + (ngay2.getMonth()+1) * 31 + ngay2.getDate();
        if (x < y) {
            return false;
        }
        return true;
    }

    /**
     * Ham kiem tra xem, neu la so tra ve true, nguoc lai la false
     *
     * @param num
     * @return
     */
    private boolean testNumber(String num) {
        boolean test = false;
        if (num.equals("") && num.length() == 0) {
            test = true;
        } else {
            for (int i = 0; i < num.length(); i++) {
                if (Character.isLetter(num.charAt(i))) {
                    test = false;
                    break;
                }
                if (i + 1 == num.length()) {
                    test = true;
                }
            }
        }
        return test;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtcmnd = new javax.swing.JTextField();
        txtGhiChu = new javax.swing.JTextField();
        txtsdt = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtHoTen = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        dtNgaysinh = new org.jdesktop.swingx.JXDatePicker();
        dtNgayvaolam = new org.jdesktop.swingx.JXDatePicker();
        cobGioiTinh = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtMaNV = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnReload = new javax.swing.JButton();
        btnLuu = new javax.swing.JButton();
        txtTimKiem = new javax.swing.JTextField();
        btnPrint = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDSNhanVien = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("QUẢN LÝ NHÂN VIÊN");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông Tin Cá Nhân"));

        txtcmnd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcmndKeyTyped(evt);
            }
        });

        txtGhiChu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGhiChuActionPerformed(evt);
            }
        });

        txtsdt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtsdtKeyTyped(evt);
            }
        });

        jLabel9.setText("CMND:");

        jLabel12.setText("Ghi Chú:");

        jLabel8.setText("Email");

        jLabel7.setText("Địa Chỉ:");

        jLabel6.setText("SĐT:");

        jLabel10.setText("Ngày Vào Làm:");

        jLabel3.setText("Họ Tên");

        jLabel5.setText("Ngày Sinh");

        dtNgayvaolam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtNgayvaolamActionPerformed(evt);
            }
        });

        cobGioiTinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));
        cobGioiTinh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cobGioiTinhActionPerformed(evt);
            }
        });

        jLabel2.setText("Mã NV");

        txtMaNV.setEditable(false);

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/user-add-icon.png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/edit-icon.png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/trash-icon.png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnReload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/Button-Refresh-icon.png"))); // NOI18N
        btnReload.setText("Reload");
        btnReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReloadActionPerformed(evt);
            }
        });

        btnLuu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/Save-icon.png"))); // NOI18N
        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        txtTimKiem.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTimKiemKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtTimKiemKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTimKiemKeyTyped(evt);
            }
        });

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/vttu/img/printer-icon.png"))); // NOI18N
        btnPrint.setText("Print");
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        jLabel4.setText("Tìm nhân viên:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                        .addComponent(btnThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLuu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnReload)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPrint))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa)
                    .addComponent(btnReload)
                    .addComponent(btnLuu)
                    .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cobGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(dtNgaysinh, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(txtEmail)
                                    .addComponent(txtsdt))))
                        .addGap(80, 80, 80)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addGap(44, 44, 44)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDiaChi)
                                    .addComponent(txtcmnd)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtGhiChu)
                                    .addComponent(dtNgayvaolam, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(88, 88, 88))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cobGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dtNgaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtsdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dtNgayvaolam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(txtGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        tblDSNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblDSNhanVien.setRowHeight(25);
        tblDSNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDSNhanVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblDSNhanVien);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setText("QUẢN LÝ THÔNG TIN NHÂN VIÊN");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(3, 3, 3)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(326, 326, 326))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(15, 15, 15)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        them = true;
        txtMaNV.setText(null);
        txtHoTen.setText(null);
        txtDiaChi.setText(null);
        txtsdt.setText(null);
        txtEmail.setText(null);
        txtGhiChu.setText(null);
        txtcmnd.setText(null);
        enableTxt(true);
        enableButton(false);
        txtHoTen.requestFocus();
    }//GEN-LAST:event_btnThemActionPerformed

    private void tblDSNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDSNhanVienMouseClicked
        int index = tblDSNhanVien.getSelectedRow();
        txtMaNV.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 0)));
        txtHoTen.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 1)));                               
        dtNgaysinh.setDate((Date) tblDSNhanVien.getValueAt(index, 2));
        String gt = String.valueOf(tblDSNhanVien.getValueAt(index, 3));
        if (gt.equals("Nam")) {
            cobGioiTinh.setSelectedItem("Nam");
        } else {
            cobGioiTinh.setSelectedItem("Nữ");
        }
        txtDiaChi.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 4)));
        txtEmail.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 6)));
        txtsdt.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 5)));
        txtcmnd.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 7)));
        dtNgayvaolam.setDate((Date) tblDSNhanVien.getValueAt(index, 8));
        txtGhiChu.setText(String.valueOf(tblDSNhanVien.getValueAt(index, 9)));
    }//GEN-LAST:event_tblDSNhanVienMouseClicked

    private void txtGhiChuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGhiChuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtGhiChuActionPerformed

    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReloadActionPerformed
        loadData();
        enableButton(true);
        enableTxt(false);
    }//GEN-LAST:event_btnReloadActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        them = false;
        enableButton(false);
        enableTxt(true);
        txtHoTen.requestFocus();
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed

        String hoten = txtHoTen.getText();
        int gioitinh = 0;
        if (cobGioiTinh.getSelectedItem().equals("Nam")) {
            gioitinh = 1;
        } else {
            gioitinh = 0;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date_ngaysinh = dtNgaysinh.getDate();
        String ngaysinh = df.format(date_ngaysinh);
        String email = txtEmail.getText();
        String diachi = txtDiaChi.getText();
        String sdt = txtsdt.getText();
        String cmnd = txtcmnd.getText();
        Date date = dtNgayvaolam.getDate();
        String ngayvaolam = df.format(date);
        String ghichu = txtGhiChu.getText();
        if (them == false) {
            int manv = Integer.valueOf(txtMaNV.getText());
            if (hoten.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập họ tên");
                txtHoTen.requestFocus();
            } else if (getAge(dtNgaysinh.getDate()) < 18) {
                JOptionPane.showMessageDialog(rootPane, "Bạn nhập tuổi nhân viên sai");
                dtNgaysinh.requestFocus();
            } else if (testNumber(txtcmnd.getText().trim()) == false && cmnd.length() < 9||cmnd.length()>13) {
                JOptionPane.showMessageDialog(rootPane, "Bạn nhập số chứng minh nhân dân sai");
                txtcmnd.requestFocus();
            } else {
                try {
                    ReadXML read = new ReadXML();
                    Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                    vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                    boolean x = common.UpdateNhanVien(manv, hoten, ngaysinh, gioitinh, diachi, sdt, email, cmnd, ngayvaolam, ghichu);
                    if (x) {
                        JOptionPane.showMessageDialog(rootPane, "Cập nhật thành công");
                        enableTxt(false);
                        enableButton(true);
                        loadData();
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Cập nhật thất bại ");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(rootPane, "Kết nối Server thất bại ");
                }
            }
        } else {
            if (hoten.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập họ tên");
                txtHoTen.requestFocus();
            } else if (getAge(dtNgaysinh.getDate()) < 18) {
                JOptionPane.showMessageDialog(rootPane, "Bạn nhập tuổi nhân viên sai");
                dtNgaysinh.requestFocus();
            } else if (testNumber(txtcmnd.getText().trim()) == false || cmnd.length() < 9|| cmnd.length()>13) {
                JOptionPane.showMessageDialog(rootPane, "Bạn nhập số chứng minh nhân dân sai");
                txtcmnd.requestFocus();
            } else {
                try {
                    ReadXML read = new ReadXML();
                    Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                    vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                    boolean x = common.insertNhanVien(hoten, ngaysinh, gioitinh, diachi, sdt, email, cmnd, ngayvaolam, ghichu);
                    if (x) {
                        JOptionPane.showMessageDialog(rootPane, "Thêm nhân viên thành công");
                        enableTxt(false);
                        enableButton(true);
                        loadData();
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Thêm nhân viên thất bại ");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(rootPane, "Kết nối Server thất bại ");
                }
            }
        }

    }//GEN-LAST:event_btnLuuActionPerformed

    private void cobGioiTinhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cobGioiTinhActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cobGioiTinhActionPerformed

    private void txtTimKiemKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTimKiemKeyTyped
        try {
            ReadXML read = new ReadXML();
            Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
            vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
            AbstractTableModel x = common.timKiem(txtTimKiem.getText());
            this.tblDSNhanVien.setModel(x);
        } catch (Exception e) {
            e.printStackTrace();
            //JOptionPane.showMessageDialog(rootPane, e);
        }
    }//GEN-LAST:event_txtTimKiemKeyTyped

    private void txtTimKiemKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTimKiemKeyPressed
        if (txtTimKiem.getText().equals("Nhập thông tin tìm kiếm")) {
            txtTimKiem.setText("");
        }
    }//GEN-LAST:event_txtTimKiemKeyPressed

    private void txtTimKiemKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTimKiemKeyReleased
        //if(txt)
    }//GEN-LAST:event_txtTimKiemKeyReleased

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        try {
            int i = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa dữ liệu này không?", "Cảnh Báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == 0) {
                ReadXML read = new ReadXML();
                Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                boolean x = common.DeleteNhanVien(Integer.parseInt(txtMaNV.getText()));

                if (x) {
                    JOptionPane.showMessageDialog(rootPane, "Xóa thành công");
                    enableTxt(false);
                    enableButton(true);
                    loadData();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Xóa thất bại ");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, "Kết nối Server thất bại ");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void dtNgayvaolamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtNgayvaolamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dtNgayvaolamActionPerformed

    private void txtcmndKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcmndKeyTyped
        int key = evt.getKeyChar();
        String st =txtcmnd.getText();
        String stTest = "0123456789";
        if (key != evt.VK_BACK_SPACE
                && key != evt.VK_DELETE
                && key != evt.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
                JOptionPane.showMessageDialog(this, "Chỉ được nhập số");
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_txtcmndKeyTyped

    private void txtsdtKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtsdtKeyTyped
        int key = evt.getKeyChar();
        String st = txtEmail.getText();
        String stTest = "0123456789";
        if (key != evt.VK_BACK_SPACE
                && key != evt.VK_DELETE
                && key != evt.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
                JOptionPane.showMessageDialog(this, "Chỉ được nhập số");
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_txtsdtKeyTyped

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
        String link = "src\\vn\\edu\\vttu\\nhanvien\\danhsachnhanvien.jrxml";
        try {
            //Tạo kết nối đến Mysql
            Class.forName("com.mysql.jdbc.Driver");
            String database = "jdbc:mysql://"+thongtindb.getIp()+":"+thongtindb.getPort()+"/"+thongtindb.getDb();
            String us = thongtindb.getU();
            String pa = thongtindb.getP();
            Map<String, Object> params = new HashMap<String, Object>();  
            Connection conn = (Connection) DriverManager.getConnection(database, us, pa);
            // tạo JasperReport
            JasperReport jr = JasperCompileManager.compileReport(link);
            //tạo JasperPrint
            JasperPrint jp = JasperFillManager.fillReport(jr, params, conn);            
            JasperViewer jv = new JasperViewer(jp, false); 
            jv.setVisible(true); 
        } catch (Exception ex) {

        }
    }//GEN-LAST:event_btnPrintActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NhanVien_QuanLy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NhanVien_QuanLy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NhanVien_QuanLy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NhanVien_QuanLy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NhanVien_QuanLy nhanVien_QuanLy = new NhanVien_QuanLy();
                nhanVien_QuanLy.setVisible(true);
                nhanVien_QuanLy.setExtendedState(nhanVien_QuanLy.getExtendedState() | JFrame.MAXIMIZED_BOTH);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnReload;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cobGioiTinh;
    private org.jdesktop.swingx.JXDatePicker dtNgaysinh;
    private org.jdesktop.swingx.JXDatePicker dtNgayvaolam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDSNhanVien;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtGhiChu;
    private javax.swing.JTextField txtHoTen;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtTimKiem;
    private javax.swing.JTextField txtcmnd;
    private javax.swing.JTextField txtsdt;
    // End of variables declaration//GEN-END:variables

}
